package com.business.action.system;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import net.sf.rose.jdbc.PageBean;
import net.sf.rose.jdbc.dao.MapDAO;
import net.sf.rose.jdbc.query.MapSQL;
import net.sf.rose.jdbc.service.Service;
import net.sf.rose.web.util.WebUtils;

/**
 * 
 * @author fengjian
 * @version 创建时间：2018年12月25日
 * 基本数据库单表的操作接口类
 */
@Controller
@RequestMapping("/com/map")
public class CommonAction {

    /**
     * 列表查询
     */
	@ResponseBody
	@RequestMapping("/list.do")
	public PageBean list(HttpServletRequest request, Service service) {
		PageBean page = new PageBean();
		MapDAO dao = new MapDAO(service);
		MapSQL query = dao.getQuerySQL();
		query.createSql(WebUtils.getRequestData(request));
		page.setData(dao.list());
		return page;
	}

    /**
     * 分页列表查询
     */
	@ResponseBody
	@RequestMapping("/page.do")
	public PageBean page(HttpServletRequest request, Service service) {
		PageBean page = WebUtils.getPageBean(request);
		MapDAO dao = new MapDAO(service);
		MapSQL query = dao.getQuerySQL();
		query.setPage(page);
		query.createSql(WebUtils.getRequestData(request));
		dao.list();
		return page;
	}

    /**
     * 根据表的主键查询
     */
	@ResponseBody
	@RequestMapping("/get.do")
	public PageBean get(HttpServletRequest request, Service service) {
		PageBean page = new PageBean();
		MapDAO dao = new MapDAO(service);
		MapSQL query = dao.getQuerySQL();
		query.createSql(WebUtils.getRequestData(request));
		page.setData(dao.get());
		return page;
	}

    /**
     * 保存表数据
     */
	@ResponseBody
	@RequestMapping("/save.do")
	public PageBean save(HttpServletRequest request, Service service) {
		PageBean page = new PageBean();
		MapDAO dao = new MapDAO(service);
		MapSQL query = dao.getQuerySQL();
		query.createSaveSql(WebUtils.getRequestData(request));
		if (dao.update() > 0) {
			page.setMessage("保存成功！");
		} else {
			page.setSuccess(false);
			page.setMessage("没有一条记录保存成功！");
		}
		return page;
	}

    /**
     * 删除表数据
     */
	@ResponseBody
	@RequestMapping("/delete.do")
	public PageBean delete(HttpServletRequest request, Service service) {
		PageBean page = new PageBean();
		MapDAO dao = new MapDAO(service);
		MapSQL query = dao.getQuerySQL();
		query.createDeleteSql(WebUtils.getRequestData(request));
		if (dao.update() > 0) {
			page.setMessage("删除成功！");
		} else {
			page.setSuccess(false);
			page.setMessage("没有一条记录删除成功！");
		}
		return page;
	}

}
